﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BUS;
using DAO;

namespace GUI
{
    public partial class frmChonThietBi : Form
    {
//Danh sach cac bien---------------------
        bool _loadXong = false;
        int _chonMach = 1;
        private int csPin;
        public int CsPin
        {
            get { return csPin; }
            set { csPin = value; }
        }
        private List<int> _dkSac = new List<int>();//1
        public List<int> DkSac
        {
            get { return _dkSac; }
            set { _dkSac = value; }
        }
        private List<int> _luuTru = new List<int>();//2
        public List<int> LuuTru
        {
            get { return _luuTru; }
            set { _luuTru = value; }
        }
        private List<int> _kichDien = new List<int>();//3
        public List<int> KichDien
        {
            get { return _kichDien; }
            set { _kichDien = value; }
        }

//-----------------------------------------
        public frmChonThietBi(int a)
        {
            InitializeComponent();
            _chonMach = a;
        }
        private void frmChonThietBi_Load(object sender, EventArgs e)
        {
            lbCS.Text = CsPin.ToString();
            CapNhatLaiTrangThai();
            if (_chonMach == 3)
            {
                btnBack_Click(null, null);
                btnNext_Click(null, null);
            }
            else
            {
                btnNext_Click(null, null);
                btnBack_Click(null, null);
            }
        }


        private void Load_MachSac()
        {
            string sql = "select STT,TenTB,MaTB,CSKhaDung,DonGia from MACHSAC";// where CSKhaDung>="+lbCS.Text;
            DataTable _dulieu = Provider.TimKiem(sql);
            ThemDuLieu(_dulieu);
        }
        private void Load_LoaiAcQuy()
        {
            AcQuyBus _acQuy = new AcQuyBus();
            cbbLoai.DataSource = _acQuy.Load_LoaiAcQuy();
            cbbLoai.DisplayMember = "TenLoai";
            cbbLoai.ValueMember = "MaLoai"; 
        }
        private void Load_AcQuy()
        {
            string sql = "select STT,TenTB,MaTB,CSKhaDung,DonGia from AcQuy where MaLoai =" + cbbLoai.SelectedValue.ToString();// + " and CSKhaDung>=" + lbCS.Text;
            DataTable _dulieu = Provider.TimKiem(sql);
            ThemDuLieu(_dulieu);
        }
        private void Load_LoaiKichDien()
        {
            LoaiBoKichDienBUS _lkd = new LoaiBoKichDienBUS();
            cbbLoai.DataSource = _lkd.LoadLoaiBoKD();
            cbbLoai.DisplayMember = "TenLoai";
            cbbLoai.ValueMember = "MaLoai";
        }
        private void Load_KichDien()
        {
            string sql = "select STT,TenTB,MaTB,CSKhaDung,DonGia from KICHDIEN where MaLoai =" + cbbLoai.SelectedValue.ToString();// +" and CSKhaDung>=" + lbCS.Text;
            DataTable _dulieu = Provider.TimKiem(sql);
            ThemDuLieu(_dulieu);
        }

        private void cbbLoai_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_loadXong == true)
            {
                CapNhatGioHang();
                XoaDuLieu();
                if (_chonMach == 2)
                {
                    Load_AcQuy();
                }
                else
                {
                    Load_KichDien();
                }
            }
        }
        private void btnXong_Click(object sender, EventArgs e)
        {
            CapNhatGioHang();
            Close();
        }
        private void btnNext_Click(object sender, EventArgs e)
        {
            if (_chonMach < 3)
            {
                CapNhatGioHang();
                _chonMach += 1;
                CapNhatLaiTrangThai();
                if(dgv.Rows.Count==0)
                    Load_AcQuy();
            }
        }
        private void btnBack_Click(object sender, EventArgs e)
        {
            if (_chonMach >1)
            {
                CapNhatGioHang();
                _chonMach -= 1;
                CapNhatLaiTrangThai();
            }
        }

        private void CapNhatLaiTrangThai()
        {
            if (_chonMach == 1)
            {
                this.Text = "Chọn Mạch Sạc";
                lbLoai.Enabled = false;
                cbbLoai.Enabled = false;

                btnBack.Enabled = false;
                Load_MachSac();
            }
            else if (_chonMach == 2)
            {
                this.Text = "Chọn Thiết Bị Lưu Trữ";
                lbLoai.Enabled = true;
                cbbLoai.Enabled = true;


                btnBack.Enabled = true;
                btnNext.Enabled = true;
                Load_LoaiAcQuy();
                _loadXong = true;
                //Load_AcQuy();
            }
            else
            {
                this.Text = "Chọn Bộ Kích Điện";
                btnNext.Enabled = false;
                Load_LoaiKichDien();
                _loadXong = true;
                //Load_KichDien();
            }
        }
        private void CapNhatGioHang()
        {
            if (_chonMach == 1)
            {
                DkSac = CapNhatGioHang(DkSac);
            }
            else if (_chonMach == 2)
            {
                LuuTru = CapNhatGioHang(LuuTru);
            }
            else
            {
                foreach (DataGridViewRow row in dgv.Rows)
                {
                    KichDien = CapNhatGioHang(KichDien);
                }
            }
            XoaDuLieu();
        }
        private void XoaDuLieu()
        {
            int n=dgv.Rows.Count-1;
            for (int i = n; i >=0; i--)
            {
                dgv.Rows.RemoveAt(i);
            }
        }
        private void ThemDuLieu(DataTable bangDuLieu)
        {
            dgv.AllowUserToAddRows = true;
            foreach (DataRow value in bangDuLieu.Rows)
            {
                DataGridViewRow _newRow = (DataGridViewRow)dgv.Rows[0].Clone();
                _newRow.Cells[0].Value = value[0];
                _newRow.Cells[1].Value = value[1];
                _newRow.Cells[2].Value = value[2];
                _newRow.Cells[3].Value = value[3];
                _newRow.Cells[4].Value = value[4];
                if (KiemTraTonTaiGioHang(int.Parse(value[0].ToString())))
                    _newRow.Cells[5].Value = true;
                //else
                //    _newRow.Cells[5].Value = true;
                dgv.Rows.Add(_newRow);
            }
            dgv.AllowUserToAddRows = false;
        }
        private bool KiemTraTonTaiGioHang(int STT)
        {
            if (_chonMach == 1)
            {
                for (int i = 0; i < DkSac.Count; i++)
                    if (DkSac[i] == STT)
                        return true;
            }
            else if (_chonMach == 2)
            {
                for (int i = 0; i < LuuTru.Count; i++)
                    if (LuuTru[i] == STT)
                        return true;
            }
            else
            {
                for (int i = 0; i < KichDien.Count; i++)
                    if (KichDien[i] == STT)
                        return true;
            }
            return false;
        }
        private List<int> CapNhatGioHang(List<int> a)
        {
            foreach (DataGridViewRow row in dgv.Rows)
            {
                int giaTri = -1;
                if (row.Cells["clCheck"].Value != null)
                {
                    giaTri = int.Parse(row.Cells["clSTT"].Value.ToString());
                    if ((Boolean)row.Cells["clCheck"].Value == true)
                    {
                        if (KiemTraTonTaiGioHang(giaTri) == false)
                            a.Add(giaTri);
                    }
                    else if (KiemTraTonTaiGioHang(giaTri) == true)
                        a.Remove(giaTri);
                }
            }
            return a;
        }

    }
}
